/*
 * @Author: yuluoY 2120942398@qq.com
 * @Date: 2024-10-28 20:53:43
 * @LastEditors: yuluoY 2120942398@qq.com
 * @LastEditTime: 2024-11-01 22:44:43
 * @FilePath: \Projecte:\gitee\GD32F427\gd32-f427\App\inc\app_key.h
 * @Description: 
 * 
 * Copyright (c) 2024 by ${YuluoY}, All Rights Reserved. 
 */

#ifndef APP_KEY_H
#define APP_KEY_H

#include "main.h"

/* 各种按键功能去抖计数器（10ms基准时间） */
#define BTN_INVALID 	            (0xFE)	/* 无效设定 */
#define BTN_DITHER 		            (1)		/* 消抖计数器20ms(10ms去抖+10ms程序状态切换) */
#define BTN_LONG_PRESS	            (100)	/* 长按1s （选择短按和长按功能时的长按时间）*/
#define BTN_SUPER_LONG_PRESS 	    (220) 	/* 超长按2.2s （选择长按功能时的长按时间）*/
#define BTN_CONTINUE_PRESS_INTERVAL (24) 	/* 连按的间隔240ms */
#define BTB_CONTINUE_PRESS_TIME 	(120)   /* 进入连按的时间1.2s */

/* 按键功能码 */
#define FUNC_SHORT 		            (0x00)  /* 仅短按 */
#define FUNC_SHORT_LONG             (0x01)  /* 短按和长按 */
#define FUNC_LONG		            (0x02)  /* 长按 */
#define FUNC_SHORT_CONTINUE         (0x03)  /* 短按和连按 */

/* 使能连按功能 */
#define ENABLE_CONTINUE_FUNC	0x01		/* 使能连按按标志位 */

/* 按键信息结构体 */
typedef struct
{
	uint8_t KeyFunc;				        /* 按键功能 */
	uint8_t Key_Value;			            /* 按键键值 */
	uint8_t Key_Status;			            /* 按键状态 */
	uint8_t Key_Hold_count; 		        /* 按键长按计数器 */
	uint8_t Key_longpress_time;		        /* 设定的长按时间 */
}key_info_t;

/* 各按键按下标志位结构体 */
typedef union
{
	uint8_t KeyPress_Flag;		            /* 按键按下标志位，抬起自动清0 */
	struct
	{
		uint8_t Key0Press:1;                /* 实际只用到了两个按键 */
		uint8_t Key1Press:1;
		uint8_t Key2Press:1;
		uint8_t Key3Press:1;
		uint8_t Key4Press:1;
		uint8_t Key5Press:1;
		uint8_t Key6Press:1;
		uint8_t Key7Press:1;
	}bits;
}uKeyPress_t;

/* 各按键长按保持标志位结构体 */
typedef union
{
	uint8_t KeyHold_Flag;		            /* 按键保持长按标志位，抬起自动清0 */
	struct
	{
		uint8_t Key0Hold:1;                 /* 实际只用到了两个按键 */
		uint8_t Key1Hold:1;
		uint8_t Key2Hold:1;
		uint8_t Key3Hold:1;
		uint8_t Key4Hold:1;
		uint8_t Key5Hold:1;
		uint8_t Key6Hold:1;
		uint8_t Key7Hold:1;
	}bits;
}uKeyHold_t;

/* 各按键释放标志位结构体 */
typedef union
{
	uint8_t KeyRelease_Flag;	            /* 按键抬起标志位，必须手动清0 */
	struct
	{
		uint8_t Key0Release:1;              /* 实际只用到了两个按键 */
		uint8_t Key1Release:1;
		uint8_t Key2Release:1;
		uint8_t Key3Release:1;
		uint8_t Key4Release:1;
		uint8_t Key5Release:1;
		uint8_t Key6Release:1;
		uint8_t Key7Release:1;
	}bits;
}uKeyRelease_t;

/* 各按键连按标志位结构体 */
typedef union
{
	uint8_t KeyContinue_Flag;	            /* 按键连按标志位，抬起自动清0（实际未使用）*/
	struct
	{
		uint8_t Key0Continue:1;             /* 实际只用到了两个按键 */
		uint8_t Key1Continue:1;
		uint8_t Key2Continue:1;
		uint8_t Key3Continue:1;
		uint8_t Key4Continue:1;
		uint8_t Key5Continue:1;
		uint8_t Key6Continue:1;
		uint8_t Key7Continue:1;
	}bits;
}uKey4Continue_t;

extern uKeyPress_t uKeyPress;
extern uKeyHold_t uKeyHold;
extern uKeyRelease_t uKeyRelease;
extern uKey4Continue_t uKeyContinue;
extern key_info_t sKey0, sKey1;

void key_task(void);
void app_key_create(void);
uint8_t app_key_readval(void);
void app_key_scanf(uint8_t l_keydata, key_info_t *key);
void app_key_setmode(uint8_t function, key_info_t *key);
void app_key_clr(void);

#endif // !APP_KEY_H
